//
//  GrowRecordDetailViewController.m
//  PigMarket
//
//  Created by mac on 2019/7/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "GrowRecordDetailViewController.h"
#import "GrowRecordDetailCell.h"
#import "GrowRecordDetailFooterView.h"

@interface GrowRecordDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSArray *dataArr;
/**
 sectionTitleArr
 */
@property (nonatomic , strong) NSArray *sectionTitleArr;
/**
 model
 */
@property (nonatomic , strong) GrowRecordDetail *growRecordDetailModel;
/**
 footerView
 */
@property (nonatomic , strong) GrowRecordDetailFooterView *footerView;
@end

@implementation GrowRecordDetailViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"报告详情";
    [self setUpView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
}

- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.mas_equalTo(0);
        make.left.and.right.top.bottom.mas_equalTo(0);
    }];
}

- (void) initAction {
    
}

- (void)initData {
    self.sectionTitleArr = @[@"",@"仔猪信息",@"本期情况",@"费用情况",@"其他"];
    @weakify(self);
    [[[PigMarketApi sharedInstance] growRecordDetailWithReportId:self.pigGrowReportId] subscribeNext:^(GrowRecordDetail * _Nullable growRecordDetail) {
        @strongify(self);
        self.growRecordDetailModel = growRecordDetail;
        NSString *varietiesName = growRecordDetail.varietiesName ? growRecordDetail.varietiesName : @"";
        NSString *num = growRecordDetail.pigNum ? growRecordDetail.pigNum : @"";
        NSString *createTime = growRecordDetail.createTime ? growRecordDetail.createTime : @"";
        NSString *dayOld = [NSString stringWithFormat:@"%@天",growRecordDetail.dayAge];
        NSString *weight = [NSString stringWithFormat:@"%.2fkg",growRecordDetail.nowWeight];
        NSString *addWeight = [NSString stringWithFormat:@"%.2fkg",growRecordDetail.weightAdd];
        NSString *anticipatedIncome = [NSString stringWithFormat:@"￥%.2f",growRecordDetail.getNow];
        NSString *allCharge = [NSString stringWithFormat:@"￥%@",growRecordDetail.payAll];
        NSString *allIncome = [NSString stringWithFormat:@"￥%.2f",growRecordDetail.getAll];
        NSString *immune = growRecordDetail.immune ? growRecordDetail.immune : @"";
        NSString *history = growRecordDetail.history ? growRecordDetail.history : @"";
        NSString *date = [NSString stringWithFormat:@"距离%@还剩%@天",growRecordDetail.expectOutDate,growRecordDetail.overDays];
        NSString *limitDay = [NSString stringWithFormat:@"%@",growRecordDetail.overDays];
        NSArray *imgArr = growRecordDetail.imgs;
        self.dataArr = @[@[@{@"title":@"",@"detail":imgArr,@"limitday":@""}],
                         @[@{@"title":@"仔猪品种",@"detail":varietiesName,@"limitday":@""},
                           @{@"title":@"仔猪编号",@"detail":num,@"limitday":@""},
                           @{@"title":@"报告日期",@"detail":createTime,@"limitday":@""},
                           @{@"title":@"日龄",@"detail":dayOld,@"limitday":@""},
                           @{@"title":@"重量",@"detail":weight,@"limitday":@""}],
                         @[@{@"title":@"本期重量增加",@"detail":addWeight,@"limitday":@""},
                           @{@"title":@"本期预收益",@"detail":anticipatedIncome,@"limitday":@""}],
                         @[@{@"title":@"费用累计",@"detail":allCharge,@"limitday":@""},
                           @{@"title":@"预收益累计",@"detail":allIncome,@"limitday":@""}],
                         @[@{@"title":@"免疫情况",@"detail":immune,@"limitday":@""},
                           @{@"title":@"换栏历史",@"detail":history,@"limitday":@""},
                           @{@"title":@"托管到期时间",@"detail":date,@"limitday":limitDay}]];
        
        self.footerView.price = [NSString stringWithFormat:@"欠费总额:￥%.2f",growRecordDetail.arrears];
        [self.myTableView reloadData];
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
    }];
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataArr[section] count];

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 195;
    } else {
        return 54;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0.01;
    } else {
        return 34;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 4) {
        return 10;
    } else {
        return 0.01;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    } else {
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 34)];
        UILabel *sectionTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH/2, 34)];
        sectionTitle.textColor = [UIColor colorWithHexString:@"#969696"];
        sectionTitle.font = [UIFont systemFontOfSize:14];
        sectionTitle.text = self.sectionTitleArr[section];
        [headerView addSubview:sectionTitle];
        return headerView;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    GrowRecordDetailCell *cell = [GrowRecordDetailCell cellWithTableView:tableView];
    if (self.dataArr.count > 0) {
        [cell configCellWithInfoDic:self.dataArr[indexPath.section][indexPath.row] index:indexPath];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark - target action

#pragma mark - private method

#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.tableFooterView = self.footerView;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, SCREEN_WIDTH, 0.01)];
    }
    return _myTableView;
}
- (NSArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSArray alloc] init];
    }
    return _dataArr;
}
- (NSArray *)sectionTitleArr {
    if (!_sectionTitleArr) {
        _sectionTitleArr = [[NSArray alloc] init];
    }
    return _sectionTitleArr;
}
- (GrowRecordDetailFooterView *)footerView {
    if (!_footerView) {
        _footerView = [[GrowRecordDetailFooterView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, 54)];
        _footerView.backgroundColor = [UIColor whiteColor];
    }
    return _footerView;
}
@end
